package org.leaf.green.b2b

class MarketPriceApprovalController {

    def marketPriceApprovalService

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/market/price/approval/list', model : [marketPriceApprovalInstanceList: marketPriceApprovalService.list(params), marketPriceApprovalInstanceTotal: marketPriceApprovalService.count()]
    }

    def create = {
        def marketPriceApprovalInstance = marketPriceApprovalService.init(params)
        //marketPriceApprovalInstance.properties = params
        render view : '/b2b/market/price/approval/create', model : [marketPriceApprovalInstance: marketPriceApprovalInstance]
    }

    def save = {
        def marketPriceApprovalInstance = new MarketPriceApproval(params)
        if (!marketPriceApprovalInstance.hasErrors() && marketPriceApprovalInstance.save()) {
            flash.message = "marketPriceApproval.created"
            flash.args = [marketPriceApprovalInstance.id]
            flash.defaultMessage = "MarketPriceApproval ${marketPriceApprovalInstance.id} created"
            redirect(action: "show", id: marketPriceApprovalInstance.id)
        }
        else {
            render(view: "/b2b/market/price/approval/create", model: [marketPriceApprovalInstance: marketPriceApprovalInstance])
        }
    }

    def show = {
        def marketPriceApprovalInstance = MarketPriceApproval.get(params.id)
        if (!marketPriceApprovalInstance) {
            flash.message = "marketPriceApproval.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceApproval not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/price/approval/show', model : [marketPriceApprovalInstance: marketPriceApprovalInstance]
        }
    }

    def edit = {
        def marketPriceApprovalInstance = MarketPriceApproval.get(params.id)
        if (!marketPriceApprovalInstance) {
            flash.message = "marketPriceApproval.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceApproval not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/price/approval/edit', model : [marketPriceApprovalInstance: marketPriceApprovalInstance]
        }
    }

    def update = {
        def marketPriceApprovalInstance = MarketPriceApproval.get(params.id)
        if (marketPriceApprovalInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketPriceApprovalInstance.version > version) {
                    
                    marketPriceApprovalInstance.errors.rejectValue("version", "marketPriceApproval.optimistic.locking.failure", "Another user has updated this MarketPriceApproval while you were editing")
                    render(view: "/b2b/market/price/approval/edit", model: [marketPriceApprovalInstance: marketPriceApprovalInstance])
                    return
                }
            }
            marketPriceApprovalInstance.properties = params
            if (!marketPriceApprovalInstance.hasErrors() && marketPriceApprovalInstance.save()) {
                flash.message = "marketPriceApproval.updated"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceApproval ${params.id} updated"
                redirect(action: "show", id: marketPriceApprovalInstance.id)
            }
            else {
                render(view: "/b2b/market/price/approval/edit", model: [marketPriceApprovalInstance: marketPriceApprovalInstance])
            }
        }
        else {
            flash.message = "marketPriceApproval.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceApproval not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def marketPriceApprovalInstance = MarketPriceApproval.get(params.id)
        if (marketPriceApprovalInstance) {
            try {
                marketPriceApprovalInstance.delete()
                flash.message = "marketPriceApproval.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceApproval ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "marketPriceApproval.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceApproval ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "marketPriceApproval.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceApproval not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
